package io.persimmon.core.module.schedule;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ScheduleTaskFactory {

    private static List<MultipleScheduleTask> scheduleTaskList = new ArrayList<>();

    public static MultipleScheduleTask create(String name) {
        return create(name, 1);
    }

    public static MultipleScheduleTask create(String name, Integer threadCount) {
        return create(name, threadCount, null, null);
    }

    public static MultipleScheduleTask create(String name, Integer threadCount, Long tickDurationMill, Integer ticksPerWheel) {
        if (Objects.isNull(threadCount) || threadCount <= 0) {
            threadCount = 1;
        }
        List<SingleScheduleImpl> list = new ArrayList<>(threadCount);
        for (int i = 0; i < threadCount; i++) {
            list.add(new SingleScheduleImpl(name, tickDurationMill, ticksPerWheel));
        }
        MultipleScheduleTask multipleScheduleTask = new MultipleScheduleTask(list);
        scheduleTaskList.add(multipleScheduleTask);
        return multipleScheduleTask;
    }

    public static void stop() {
        for (MultipleScheduleTask scheduleTask : scheduleTaskList) {
            scheduleTask.stop();
        }
    }

}
